package com.shine.app;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shine.app.commons.LocationUtil;

public class BaseActivity extends Activity {
	protected AppContext appContext;
	protected int screenWidth;
	protected int screenHeight;
	protected LayoutInflater inflater;
	protected InputMethodManager inputMethodManager;
	protected ImageLoader imageLoader = null;
	protected LocationUtil locationUtil = null;
	protected Animation buttonAnimations = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getAppManager().addActivity(this);
		initGeneralParam();
	}

	protected void initGeneralParam() {
		appContext = (AppContext) getApplication();
		inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		inflater = LayoutInflater.from(this);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;

		imageLoader = ImageLoader.getInstance();
		locationUtil = new LocationUtil(this, appContext);
		buttonAnimations = AnimationUtils.loadAnimation(this,
				R.anim.anim_button);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
		}
		return super.onKeyDown(keyCode, event);
	}

	protected String getTextViewValues(TextView view) {
		String value = view.getText().toString().trim();
		return value;
	}

	protected String getEditTextValues(EditText view) {
		String value = view.getText().toString().trim();
		return value;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public void getMyLocation() {
		if (appContext.getLatitude() == 0 && appContext.getLongitude() == 0) {
			Location location = locationUtil.getLastBestLocation();
			if (location != null) {
				appContext.setLatitude(location.getLatitude());
				appContext.setLongitude(location.getLongitude());
			}
		}
	}

}
